<?php
/**
 * XIE--Scorpion
 */

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB,Session,Url,Input,Redirect;
use App\Models\admin\Position;
use App\Http\Controllers\Controller;
/**
* 
*/
class PositionController extends Controller
{
	/*
	 *职位列表
	 */
	public function positionList()
	{
        $list=Position::posits();
        $list=$this->digui($list,0,0);
        //var_dump($list);
		return view('admin/positionlist',compact('list'));

	}
     
    /*
     *添加职位
     */
	public function positionAdd(Request $request)
	{
		//判断操作
        if($request->has('_token')){
        	
        	$all=$request->input();
        	unset($all['_token']);
        	//var_dump($all);die;
        	if(Position::positAdd($all)){

        		echo '添加成功'.Redirect::action('admin\PositionController@positionList');
        	}else{

        		echo '添加失败'.Redirect::action('admin\PositionController@positionAdd');
        	}
        }else{
            
            $list=Position::posits();
            $list=$this->digui($list,0,0);

		    return view('admin/positionadd',compact('list'));
        	
        }

	}
    
	/*
	 *修改职位
	 */
	public function positionUpdate(Request $request)
	{
        //获取修改职位的id
        $id=$request->input('id');
        $do=$request->input('do');
        $val=$request->input('val');
        $zd=$request->input('zd');

        if($do=='save'){
            //修改
            if(Position::updateRow($id,$val,$zd)){
                echo '1';
            }else{
                echo '0';
            }
        }
        if($do=='del'){
            if(Position::deleteRow($id)){
                echo '1';
            }else{
                echo '0';
            }
        }

	}

	/*
	 *递归
	 */
    public function digui($data,$path,$leval)
    {
    	static $arr=array();
    	foreach($data as $v)
        {
            if($v->position_parent==$path)
            {
                $v->leval=$leval;
                $arr[]=$v;
                $this->digui($data,$v->position_id,$leval+1);
            }
        }
        return $arr;

    }



    
    
}
?>